using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using Microsoft.Surface;
using Microsoft.Surface.Presentation;
using Microsoft.Surface.Presentation.Controls;
using System.Windows;
using System.Windows.Controls;
using MapSurface.AssemblyReflexion;
namespace MapSurface.Engine
{
    public class XAMLLoader
    {/*
        #region Loader properties
        public string fileName;
        public Object rootUIElement;
        private AssemblyReflexion.AssemblyReflexion _assembly;
        public XmlDocument docxml = null;
        public AssemblyReflexion.AssemblyReflexion Assembly
        {
            get
            {
                return _assembly;
            }
            set
            {
                _assembly = value;
            }
        }

        #endregion

        #region Contructors
        public AssemblyReflexion.AssemblyReflexion MyAssembly
        {
            get
            {
                return _assembly;
            }
            set 
            {
                _assembly = value;
            }
        }
  
        public XAMLLoader(string fileName)
        {
            _assembly = new AssemblyReflexion.AssemblyReflexion(@"..\..\Assembly\Microsoft.Surface.Presentation.dll");
            docxml = new XmlDocument();
            docxml.Load(fileName);
        }
        #endregion
        #region Loader
        /// <summary>
        /// 
        /// </summary>
        /// <returns>Root of the UI Model instance</returns>
        public Object process()
        {
            Object objectUI = null;
            if (docxml.HasChildNodes)
            {
                XmlNode nodei = docxml.FirstChild;
                do
                {
                    if (nodei != null)
                    {
                        Object obj = visit(nodei, objectUI);
                        if (objectUI == null && obj != null)
                            objectUI = obj;
                        else
                            ((ItemsControl)objectUI).Items.Add(obj);
                        nodei = nodei.NextSibling;
                    }
                }
                while (nodei != null);
            }
            return objectUI;
        }

        
       public Object visit(XmlNode current, Object container)
        {
            string name=current.Name;
            Object objectUI = _assembly.getInstance(name,current);
            //objectUI = assembly.setAttributes(objectUI, current);
            if (container == null )
                container = objectUI;
            if (name.Contains("SurfaceWindow"))
            {
                container = null;
                objectUI = null;
            }
            if (current.HasChildNodes)
            {
                XmlNode nodei=current.FirstChild;
                if (nodei != null)
                    do
                    {
                        Object obj = null;
                        obj = visit(nodei, objectUI);
                        objectUI = addChild(objectUI, obj);
                        nodei = nodei.NextSibling;
                    }
                    while (nodei != null);
            }
            return objectUI;
        }

        public Object addChild(Object objectUI, Object obj)
        {
            if (objectUI != null)
            {
                if (isItemControl(objectUI))
                {
                    if (obj != null && objectUI != null)
                        ((ItemsControl)objectUI).Items.Add(obj);
                }
                else if (isPanel(objectUI))
                {
                    if (obj != null)
                        ((Panel)objectUI).Children.Add((UIElement)obj);
                }
                else if (isContentControl(objectUI))
                {
                    if (obj != null)
                        ((ContentControl)objectUI).Content = obj;
                }
            }
            if (objectUI == null && obj != null)
                objectUI = obj;
            return objectUI;
        }
        
        #endregion

        #region finder methods
        /// <summary>
        /// This Method find and instance of a given object into UI Model
        /// </summary>
        /// <param name="objectUI"></param>
        /// <param name="element"></param>
        /// <returns></returns>
        public Object find(Object objectUI, Object element)
        {
            if (objectUI != null && (objectUI.GetHashCode() == element.GetHashCode()))
                return objectUI;
            else
            {
                if (isItemControl(objectUI))
                {
                    ItemsControl ic = ((ItemsControl)objectUI);
                    foreach (ItemCollection i in ic.Items)
                    {
                        return find(i, element);
                    }
                }
                else if (isPanel(objectUI))
                {
                    Panel p = (Panel)objectUI;
                    foreach (UIElement pi in p.Children)
                    {
                        return find(pi, element);
                    }
                }
                else if (isContentControl(objectUI))
                {
                    ContentControl cc = (ContentControl)objectUI;
                    return find(cc.Content, element);
                }
            }
            return null;
        }

        /// <summary>
        /// This Method find and instance of a given object into UI Model
        /// </summary>
        /// <param name="objectUI"></param>
        /// <param name="element"></param>
        /// <returns></returns>
        public bool contains(Object objectUI, Object element)
        {
            if (objectUI != null && (objectUI.GetHashCode() == element.GetHashCode()))
                return true;
            else
            {
                if (isItemControl(objectUI))
                {
                    ItemsControl ic = ((ItemsControl)objectUI);
                    foreach (ItemCollection i in ic.Items)
                    {
                        if (contains(i, element))
                            return true;
                    }
                }
                else if (isPanel(objectUI))
                {
                    Panel p = (Panel)objectUI;
                    foreach (UIElement pi in p.Children)
                    {
                        if (contains(pi, element))
                            return true;
                    }
                }
                else if (isContentControl(objectUI))
                {
                    ContentControl cc = (ContentControl)objectUI;
                    if (contains(cc.Content, element))
                        return true;
                }
            }
            return false;
        }

        public Boolean isItemControl(Object element)
        {
            try
            {

               ItemsControl obj= (ItemsControl)element;
                return true;
            }
            catch (InvalidCastException e)
            {
#if DEBUG
                Console.Out.WriteLine(e.Message);
#endif
                return false;
            }
        }
        public Boolean isPanel(Object element)
        {
            try
            {
                Panel p=(Panel)element;
                return true;
            }
            catch (InvalidCastException e)
            {
#if DEBUG
                Console.Out.WriteLine(e.Message);
#endif

                return false;
            }
        }
        public Boolean isContentControl(Object element)
        {
            try
            {
               ContentControl c= (ContentControl)element;
                return true;
            }
            catch (InvalidCastException)
            {
                return false;
            }
        }
        #endregion
        */
    }
       
}